import QtQuick 2.7
import QtQuick.Controls 2.0
import QtQuick.Layouts 1.3
import Qt.labs.folderlistmodel 2.1

Item {
    id: root

    readonly property url rootFolder: "file:///home/help"
    signal select(string source);

    function type4Icon(isDir, suffix) {
        var types = ["folder", "mp3"];
        if(suffix.lastIndexOf(".") > -1)
            suffix = suffix.substr(suffix.lastIndexOf(".") + 1);
        var type = 0;
        if(!isDir) {
            type = types.indexOf(suffix);
        }
        return types[type];
    }

    ListView {
        anchors.fill: parent
        model: FolderListModel {
            id: folders1
            rootFolder: root.rootFolder
            folder: root.rootFolder
            showDirsFirst: true
            nameFilters: [ "*.mp3", "*.aac" ]
        }

        header: ItemDelegate {
            width: parent.width
            onClicked: {
                if(folders1.folder != root.rootFolder)
                    folders1.folder = folders1.parentFolder
            }
            contentItem: Label {
                Layout.fillWidth: true
                font.pointSize: 24
                color: "white"
                elide: Text.ElideRight
                text: folders1.folder == root.rootFolder ? qsTr("根目录") : qsTr("上一级")
            }
        }

        delegate: ItemDelegate {
            id: playlistItem
            width: parent.width
            onClicked:{
                if(fileIsDir)
                    folders1.folder = fileURL
                else
                    root.select(fileURL)
            }

            contentItem: RowLayout {
                spacing: 16

                Image {
                    source: String("qrc:/images/file_%1.png").arg(type4Icon(fileIsDir, fileSuffix))
                }

                Label {
                    Layout.fillWidth: true
                    font.pointSize: 24
                    color: "white"
                    elide: Text.ElideRight
                    text: fileName
                }
            }
        }
    }
}
